package com.atguigu.yygh.hosp.mongoTest;

import com.atguigu.yygh.common.R;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @Author zhmwi
 * @Date 2021/7/7
 * @Dersciption
 */
@RestController
@RequestMapping("/mongo1")
public class MongoController {
    @Autowired
    private MongoTemplate mongoTemplate;
    //添加文档
    @GetMapping("/create")
    public void createUser(){
        User user = new User();
        user.setName("沙师弟2");
        user.setAge(12);
        user.setEmail("ssd@qq.com");
        User userMongo = mongoTemplate.insert(user);
        System.out.println("userMongo = " + userMongo);
    }
    //查询所有
    @GetMapping("/findAll")
    public R findAllUser(){
        List<User> users = mongoTemplate.findAll(User.class);
        return R.ok().data("users",users);
    }
    //根据id查询
    @GetMapping("queryById")
    public R queryById(){
        User user = mongoTemplate.findById("60e4f0b3e123184fa7d73969", User.class);
        return R.ok().data("user",user);
    }
    //条件查询
    @GetMapping("queryByCondition")
    public R queryByCondition(){
        Query query = new Query(Criteria.where("name").is("孙悟空")
                                        .and("age").is(518));
        List<User> users = mongoTemplate.find(query, User.class);
        return R.ok().data("users",users);
    }
    //模糊查询
    @GetMapping("queryLike")
    public R findUsersLikeName(){
        String name = "孙";
        String regex = String.format("%s%s%s","^.*",name,".*$");
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Query query = new Query(Criteria.where("name").regex(pattern));
        List<User> users = mongoTemplate.find(query, User.class);
        return R.ok().data("users",users);
    }
    //分页查询
    @GetMapping("findByPage")
    public R findByPage(){
        String name = "孙";
        int pageNo = 1;
        int pageSize = 2;
        Query query = new Query();
        String regex = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        query.addCriteria(Criteria.where("name").regex(pattern));
        long count = mongoTemplate.count(query, User.class);
        List<User> users = mongoTemplate.find(query.skip((pageNo - 1) * pageSize).limit(pageSize), User.class);
        HashMap<String, Object> pageMap = new HashMap<>();
        pageMap.put("list",users);
        pageMap.put("totalCount",count);
        System.out.println(pageMap);
        return R.ok();
    }
    //修改
    @GetMapping("update")
    public void updateUser(){
        User user = mongoTemplate.findById("60e4f61f1bcbbc4b16e2965c",User.class);
        user.setName("嫦娥");
        user.setAge(16);
        user.setEmail("ce@qq.com");
        Query query = new Query(Criteria.where("_id").is(user.getId()));
        Update update = new Update();
        update.set("name",user.getName());
        update.set("age",user.getAge());
        update.set("email",user.getEmail());
        UpdateResult result = mongoTemplate.upsert(query, update, User.class);
        long modifiedCount = result.getModifiedCount();
        System.out.println("modifiedCount = " + modifiedCount);
    }
    //删除操作
    @GetMapping("delete")
    public void delete(){
        Query query = new Query(Criteria.where("_id").is("60e4f0b3e123184fa7d73969"));
        DeleteResult res = mongoTemplate.remove(query, User.class);
        long count = res.getDeletedCount();
        System.out.println("count = " + count);
    }
}
